﻿#define  _CRT_SECURE_NO_WARNINGS 1

#include "List.h"

//给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。

struct ListNode
{
	int val;
	struct ListNode* next;
};

//思路一：翻指针方向
/*
struct ListNode* reverseList(struct ListNode* head)
{
	if (head == NULL)
		return NULL;
	//初始条件
	struct ListNode* n1 = NULL;
	struct ListNode* n2 = head;
	struct ListNode* n3 = n2->next;
	//结束条件
	while (n2)
	{
		//迭代过程
		n2->next = n1;
		n1 = n2;
		n2 = n3;
		if(n3)
			n3 = n3->next;
	}
	return n1;
}*/

//思路二：头插法
struct ListNode* reverseList(struct ListNode* head)
{
	struct ListNode* cur = head;
	struct ListNode* newHead = NULL;
	while (cur)
	{
		struct ListNode* next = cur->next;
		
		//头插
		cur->next = newHead;
		newHead = cur;
		cur = next;
	}
	return newHead;
}